using System;
using System.IO;
using System.Windows.Forms;

namespace FileMerger
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnSelect1_Click(object sender, EventArgs e)
        {
            SelectFile(txtFile1);
        }

        private void btnSelect2_Click(object sender, EventArgs e)
        {
            SelectFile(txtFile2);
        }

        private void SelectFile(TextBox targetBox)
        {
            using (var dialog = new OpenFileDialog())
            {
                dialog.Title = "选择要合并的文件";
                dialog.Filter = "所有文件|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    targetBox.Text = dialog.FileName;
                }
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txtFile1.Text) || 
                    string.IsNullOrWhiteSpace(txtFile2.Text))
                {
                    MessageBox.Show("请先选择两个文件");
                    return;
                }

                var content1 = File.ReadAllText(txtFile1.Text);
                var content2 = File.ReadAllText(txtFile2.Text);

                var dataDir = Path.Combine(Application.StartupPath, "Data");
                Directory.CreateDirectory(dataDir);

                var newFileName = $"merged_{DateTime.Now:yyyyMMddHHmmss}.txt";
                var outputPath = Path.Combine(dataDir, newFileName);

                File.WriteAllText(outputPath, $"{content1}\n{content2}");
                
                MessageBox.Show($"文件合并成功！\n保存路径：{outputPath}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作失败：{ex.Message}");
            }
        }
    }
}